rm(list=ls())
library(tidyverse)
library(here)

here::i_am("dot_code_figs/fig-6.R")
anes_ss <- read_csv("dot_code_figs/ANES files/anes_specialstudy_2020-2022_socialmedia_csv_20230705.csv")

## tasks 4a and 4c
ap20 <-anes_ss %>%
  select(wt=weight_post, 
         pid7=w2pid7x, 
         FT_dem=w2ftdem, 
         FT_rep=w2ftrep, 
         FT_2_dem_dem=w2tpt_d_d, 
         FT_2_dem_rep=w2tpt_d_r,
         FT_2_rep_dem=w2tpt_r_d, 
         FT_2_rep_rep=w2tpt_r_r) %>%
  subset(pid7>0 & pid7!=4)%>%
subset(FT_dem >=0 & FT_rep >=0)%>%
  sapply(as.numeric) %>% data.frame()

###calculate party dummy

ap20$pid_dum <- ifelse(ap20$pid7 > 4, "Republican", "Democrat")
ap20$in_party_FT <- ifelse(ap20$pid_dum=="Republican", ap20$FT_rep, ap20$FT_dem)
ap20$out_party_FT <- ifelse(ap20$pid_dum=="Republican", ap20$FT_dem, ap20$FT_rep)
ap20$AP <- ap20$in_party_FT - ap20$out_party_FT

##weighted average AP, then by party
ap20$wt <- as.numeric(ap20$wt) #was character for some reason

av <- ap20 %>%
  summarise(mean = weighted.mean(AP, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All")

av_party <- ap20 %>%
  group_by(pid_dum) %>%
  summarise(mean= weighted.mean(AP, wt),
            se=sd(AP)/sqrt(n()))

av_2020 <- rbind(av, av_party)

ggplot(av_2020, aes(x=pid_dum, y=mean, color=pid_dum)) +
  geom_point(size=2,aes(shape=pid_dum)) +
  theme_bw() +
  scale_y_continuous(limits=c(20,70)) +
  geom_errorbar(aes(ymin=mean-se*1.96, ymax=mean+se*1.96),
                width=0.1) +
  scale_color_manual(values=c("#545454", "black", "grey"))+
  labs(x="", y="Subgroup Mean AP", color="Party", shape="Party") +
  theme(legend.position = c(0.2,0.2), 
        legend.background = element_rect(color = NA, fill = NA)) 
#ggsave("figs/social_mean_study_AP_2020.png")


### second order
av2_dem_dem <- ap20 %>%
  subset(FT_2_dem_dem >=0) %>%
  summarise(mean = weighted.mean(FT_2_dem_dem, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All",
         belief = "Belief About Dems. Rating of Dems.")


av2_rep_dem <- ap20 %>%
  subset(FT_2_rep_dem >=0) %>%
  summarise(mean = weighted.mean(FT_2_rep_dem, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All",
         belief = "Belief About Reps. Rating of Dems.")

av2_dem_rep <- ap20 %>%
  subset(FT_2_dem_rep >=0)%>%
  summarise(mean = weighted.mean(FT_2_dem_rep, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All",
         belief = "Belief About Dems. Rating of Reps.")


av2_rep_rep <- ap20 %>%
  subset(FT_2_rep_rep >=0)%>%
  summarise(mean = weighted.mean(FT_2_rep_rep, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All",
         belief = "Belief About Reps. Rating of Reps.")


av2_dem_dem_party <- ap20 %>%
  subset(FT_2_dem_dem >=0)%>%
  group_by(pid_dum) %>%
  summarise(mean = weighted.mean(FT_2_dem_dem, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(
         belief = "Belief About Dems. Rating of Dems.")


av2_rep_dem_party <- ap20 %>%
  subset(FT_2_rep_dem >=0)%>%
  group_by(pid_dum) %>%
  summarise(mean = weighted.mean(FT_2_rep_dem, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(
         belief = "Belief About Reps. Rating of Dems.")

av2_dem_rep_party <- ap20 %>%
  subset(FT_2_dem_rep >=0)%>%
  group_by(pid_dum) %>%
  summarise(mean = weighted.mean(FT_2_dem_rep, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(
         belief = "Belief About Dems. Rating of Reps.")


av2_rep_rep_party <- ap20 %>%
  subset(FT_2_rep_rep >=0)%>%
  group_by(pid_dum) %>%
  summarise(mean = weighted.mean(FT_2_rep_rep, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(belief = "Belief About Reps. Rating of Reps.")


av_2020_party <- rbind(av2_dem_dem, av2_dem_rep, av2_rep_dem, av2_rep_rep,
                       av2_dem_dem_party, av2_dem_rep_party, av2_rep_dem_party,
                       av2_rep_rep_party)

true_dem_ft_dem <- ap20 %>%
  subset(pid_dum=="Democrat") %>%
  summarise(mean = weighted.mean(FT_dem, wt)) %>%
  mutate(belief="Belief About Dems. Rating of Dems.",
         pid_dum = "Democrat")


true_dem_ft_rep <- ap20 %>%
  subset(pid_dum=="Republican") %>%
  summarise(mean = weighted.mean(FT_dem, wt)) %>%
  mutate(belief="Belief About Reps. Rating of Dems.",
         pid_dum = "Republican")


true_rep_ft_rep <- ap20 %>%
  subset(pid_dum=="Republican") %>%
  summarise(mean = weighted.mean(FT_rep, wt)) %>%
  mutate(belief="Belief About Reps. Rating of Reps.",
         pid_dum = "Republican")


true_rep_ft_dem <- ap20 %>%
  subset(pid_dum=="Democrat") %>%
  summarise(mean = weighted.mean(FT_rep, wt)) %>%
  mutate(belief="Belief About Dems. Rating of Reps.",
         pid_dum = "Democrat")

truth <- rbind(true_dem_ft_dem, true_dem_ft_rep, true_rep_ft_dem, true_rep_ft_rep)


a <- ggplot(av_2020_party, aes(pid_dum, mean, color=pid_dum)) +
  geom_point(aes(shape=pid_dum)) +
  facet_wrap(vars(belief)) +
  theme_bw() +
  geom_errorbar(aes(ymin=mean-se*1.96, ymax=mean+se*1.96), width=0.1) +
 geom_point(data=truth, aes(pid_dum, mean), shape=1, size=3, color="#545454") +
  scale_color_manual(values=c("#545454", "black", "grey")) +
  labs(x="", y="Subgroup Mean Belief", color="", shape="",
       caption="ANES 2020 Social Media Study, N=4,732") +
  theme(legend.position = "bottom")
a

#pdf(file="C:/Users/sarah/Dropbox/Advances in Political Psychology Article/ANES 2024 Data/figure-6.pdf",
 #   height = 6,
  #  width = 10)
#ggarrange(a,
 #         nrow = 1, ncol = 1, common.legend = T, legend = "bottom")
#dev.off() 

png(file = "dot_code_figs/figs/figure-6.png",
    height = 8.5,
    width = 11,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(a,
                nrow = 1, ncol = 1, common.legend = T, legend = "bottom"))  

dev.off()



## tasks 4b and 4d
ap22 <- anes_ss %>%
  select(pid7=w3pid7x,
         FT_dem=w3ftdem,
         FT_rep=w3ftrep,
         FT_2_dem_dem=w3tpt_d_d, 
         FT_2_dem_rep=w3tpt_d_r, 
         FT_2_rep_dem=w3tpt_r_d,
         FT_2_rep_rep=w3tpt_r_r,
         wt=w3weight) %>%
  subset(pid7>0 & pid7!=4)%>%
  subset(FT_dem >=0 & FT_rep >=0)%>%
  sapply(as.numeric) %>% data.frame()


###calculate party dummy

ap22$pid_dum <- ifelse(ap22$pid7 > 4, "Republican", "Democrat")
ap22$in_party_FT <- ifelse(ap22$pid_dum=="Republican", ap22$FT_rep, ap22$FT_dem)
ap22$out_party_FT <- ifelse(ap22$pid_dum=="Republican", ap22$FT_dem, ap22$FT_rep)
ap22$AP <- ap22$in_party_FT - ap22$out_party_FT

##weighted average AP, then by party

av <- ap22 %>%
  summarise(mean = weighted.mean(AP, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All")

av_party <- ap22 %>%
  group_by(pid_dum) %>%
  summarise(mean= weighted.mean(AP, wt),
            se=sd(AP)/sqrt(n()))

av_2022 <- rbind(av, av_party)

ggplot(av_2022, aes(x=pid_dum, y=mean, color=pid_dum)) +
  geom_point(size=2,aes(shape=pid_dum)) +
  theme_bw() +
  scale_y_continuous(limits=c(20,70)) +
  geom_errorbar(aes(ymin=mean-se*1.96, ymax=mean+se*1.96),
                width=0.1) +
  scale_color_manual(values=c("#545454", "black", "grey"))+
  labs(x="", y="Subgroup Mean AP", color="Party", shape="Party") +
  theme(legend.position = c(0.2,0.2), 
        legend.background = element_rect(color = NA, fill = NA)) 
ggsave("figs/social_mean_study_AP_2022.png")


### second order
av2_dem_dem <- ap22 %>%
  subset(FT_2_dem_dem>=0)%>%
  summarise(mean = weighted.mean(FT_2_dem_dem, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All",
         belief = "Belief About Dems Rating Dems")


av2_rep_dem <- ap22 %>%
  subset(FT_2_rep_dem>=0)%>%
  summarise(mean = weighted.mean(FT_2_rep_dem, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All",
         belief = "Belief About Reps Rating Dems")

av2_dem_rep <- ap22 %>%
  subset(FT_2_dem_rep>=0)%>%
  summarise(mean = weighted.mean(FT_2_dem_rep, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All",
         belief = "Belief About Dems Rating Reps")


av2_rep_rep <- ap22 %>%
  subset(FT_2_rep_rep>=0)%>%
  summarise(mean = weighted.mean(FT_2_rep_rep, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(pid_dum = "All",
         belief = "Belief About Reps Rating Reps")


av2_dem_dem_party <- ap22 %>%
  subset(FT_2_dem_dem>=0)%>%
  group_by(pid_dum) %>%
  summarise(mean = weighted.mean(FT_2_dem_dem, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(
    belief = "Belief About Dems Rating Dems")


av2_rep_dem_party <- ap22 %>%
  subset(FT_2_rep_dem>=0)%>%
  group_by(pid_dum) %>%
  summarise(mean = weighted.mean(FT_2_rep_dem, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(
    belief = "Belief About Reps Rating Dems")

av2_dem_rep_party <- ap22 %>%
  subset(FT_2_dem_rep>=0)%>%
  group_by(pid_dum) %>%
  summarise(mean = weighted.mean(FT_2_dem_rep, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(
    belief = "Belief About Dems Rating Reps")


av2_rep_rep_party <- ap22 %>%
  subset(FT_2_rep_rep>=0)%>%
  group_by(pid_dum) %>%
  summarise(mean = weighted.mean(FT_2_rep_rep, wt),
            se= sd(AP)/sqrt(n())) %>%
  mutate(belief = "Belief About Reps Rating Reps")


av_2022_party <- rbind(av2_dem_dem, av2_dem_rep, av2_rep_dem, av2_rep_rep,
                       av2_dem_dem_party, av2_dem_rep_party, av2_rep_dem_party,
                       av2_rep_rep_party)

true_dem_ft_dem <- ap22 %>%
  subset(pid_dum=="Democrat") %>%
  summarise(mean = weighted.mean(FT_dem, wt)) %>%
  mutate(belief="Belief About Dems Rating Dems",
         pid_dum = "Democrat")


true_dem_ft_rep <- ap22 %>%
  subset(pid_dum=="Republican") %>%
  summarise(mean = weighted.mean(FT_dem, wt)) %>%
  mutate(belief="Belief About Reps Rating Dems",
         pid_dum = "Republican")


true_rep_ft_rep <- ap22 %>%
  subset(pid_dum=="Republican") %>%
  summarise(mean = weighted.mean(FT_rep, wt)) %>%
  mutate(belief="Belief About Reps Rating Reps",
         pid_dum = "Republican")


true_rep_ft_dem <- ap22 %>%
  subset(pid_dum=="Democrat") %>%
  summarise(mean = weighted.mean(FT_rep, wt)) %>%
  mutate(belief="Belief About Dems Rating Reps",
         pid_dum = "Democrat")

truth <- rbind(true_dem_ft_dem, true_dem_ft_rep, true_rep_ft_dem, true_rep_ft_rep)


ggplot(av_2022_party, aes(pid_dum, mean, color=pid_dum)) +
  geom_point(size=2, aes(shape=pid_dum)) +
  facet_wrap(vars(belief)) +
  theme_bw() +
  geom_errorbar(aes(ymin=mean-se*1.96, ymax=mean+se*1.96), width=0.1) +
  geom_point(data=truth, aes(pid_dum, mean), shape=1, size=3, color="#545454") +
  scale_color_manual(values=c("#545454", "black", "grey")) +
  labs(x="", y="Subgroup Mean Belief", color="Party", shape="Party",
       title="ANES 2022 Second Order Beliefs and True Values")
ggsave("figs/anes2022-second-order-beliefs.png", width=6)


